package com.transaction.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.transaction.config.PageBean;
import com.transaction.config.R;
import com.transaction.domain.SysShareholder;
import com.transaction.domain.SysStockInfo;
import com.transaction.domain.SysStockShareholder;
import com.transaction.service.SysShareholderService;
import com.transaction.service.SysStockInfoService;
import com.transaction.service.SysStockShareholderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("stockinfo")
public class StockController {

    @Autowired
    SysStockInfoService sysStockInfoService;
    @Autowired
    SysStockShareholderService sysStockShareholderService;

    @PostMapping("getlist")
    public R getlist(@RequestBody SysStockInfo sysStockInfo){

        PageInfo<SysStockInfo> getformlist = sysStockInfoService.getformlist(sysStockInfo);
        return R.successed(getformlist);
    }

    @PostMapping("insert")
    public R insert(@RequestBody SysStockInfo sysStockInfo){
        sysStockInfoService.save(sysStockInfo);
        ArrayList<SysStockShareholder> arrayList = new ArrayList<>();
        for (String id : sysStockInfo.getIds()) {
            SysStockShareholder sysStockShareholder = new SysStockShareholder();
            sysStockShareholder.setStockId(sysStockInfo.getId());
            sysStockShareholder.setShareholderNum(id);
            arrayList.add(sysStockShareholder);
        }
        sysStockShareholderService.saveBatch(arrayList);
        return R.successed();
    }

    @PostMapping("update")
    public R update(@RequestBody SysStockInfo sysStockInfo){
        sysStockInfoService.updateById(sysStockInfo);
        //先删除中间表的数据
        QueryWrapper<SysStockShareholder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stock_id",sysStockInfo.getId());
        sysStockShareholderService.remove(queryWrapper);
        //重新添加中间表的数据
        ArrayList<SysStockShareholder> arrayList = new ArrayList<>();
        for (String id : sysStockInfo.getIds()) {
            SysStockShareholder sysStockShareholder = new SysStockShareholder();
            sysStockShareholder.setStockId(sysStockInfo.getId());
            sysStockShareholder.setShareholderNum(id);
            arrayList.add(sysStockShareholder);
        }
        sysStockShareholderService.saveBatch(arrayList);
        return R.successed();

    }

    @DeleteMapping("del")
    public R del(Integer id){
        sysStockInfoService.removeById(id);
        QueryWrapper<SysStockShareholder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stock_id",id);
        sysStockShareholderService.remove(queryWrapper);
        return R.successed();
    }


}
